package com.bjbn.controller;

import com.bjbn.dto.WalkwayStatusDto;
import com.bjbn.service.WalkwayStatusService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 步道梯状态查询API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/walkway")
@CrossOrigin(origins = "*")
@Tag(name = "步道梯状态查询", description = "步道梯设备状态查询相关接口")
public class WalkwayStatusController {

    @Autowired
    private WalkwayStatusService walkwayStatusService;

    /**
     * 查询所有步道梯实时状态
     * @return 步道梯状态列表
     */
    @Operation(summary = "查询所有步道梯状态", description = "获取所有步道梯设备的实时状态信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/status")
    public Map<String, Object> getAllWalkwayStatus() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            List<WalkwayStatusDto> statusList = walkwayStatusService.getAllWalkwayStatus();
            
            result.put("code", 200);
            result.put("message", "查询成功");
            result.put("data", statusList);
            result.put("total", statusList.size());
            
            log.info("查询所有步道梯状态成功，共{}个设备", statusList.size());
            
        } catch (Exception e) {
            log.error("查询所有步道梯状态异常", e);
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            result.put("data", null);
            result.put("total", 0);
        }
        
        return result;
    }

    /**
     * 根据设备编码查询步道梯状态
     * @param deviceNum 设备编码
     * @return 步道梯状态
     */
    @Operation(summary = "根据设备编码查询步道梯状态", description = "根据设备编码查询指定步道梯的实时状态信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功"),
            @ApiResponse(responseCode = "404", description = "未找到指定设备"),
            @ApiResponse(responseCode = "500", description = "查询失败")
    })
    @GetMapping("/status/{deviceNum}")
    public Map<String, Object> getWalkwayStatusByDeviceNum(
            @Parameter(description = "设备编码", required = true, example = "WALK001")
            @PathVariable String deviceNum) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            WalkwayStatusDto statusDto = walkwayStatusService.getWalkwayStatusByDeviceNum(deviceNum);
            
            if (statusDto != null) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", statusDto);
                
                log.info("查询步道梯{}状态成功", deviceNum);
            } else {
                result.put("code", 404);
                result.put("message", "未找到设备编码为" + deviceNum + "的步道梯设备");
                result.put("data", null);
                
                log.warn("未找到步道梯设备：{}", deviceNum);
            }
            
        } catch (Exception e) {
            log.error("查询步道梯{}状态异常", deviceNum, e);
            result.put("code", 500);
            result.put("message", "查询失败：" + e.getMessage());
            result.put("data", null);
        }
        
        return result;
    }
} 